<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/12 0012
 * Time: 17:27
 */

namespace app\home\controller;

use app\home\model\BfNewslist;
use app\home\model\BfNewslanmu;


class News extends BaseController
{
    //实例化
    
    public function initialize(){
        $this -> _G['webseo'] = $this -> getSEOinfo();
        $this -> _G['webinfo'] = $this -> getWebinfo();
        
        $this -> assign('_G',$this -> _G);
    }
    //新闻列表
    public function index(){
        $id = $this -> request ->param('id');
        
        $data = BfNewslist :: where(['id'=>$id]) -> find();
        $lanmu = BfNewslanmu :: where(['id' =>$data['lanmuid']]) -> find();
        $this -> assign('data',$data);
        $this -> assign('lanmu',$lanmu);
        return $this->fetch($lanmu ->neitemp);
    }
    //获取列表数据接口
    public function getListByType(){
        if(input('type')){
            $list = $this->_news->getListSelect(['classify'=>1,'type_id'=>input('type'),'flag'=>1],true);
        }else{
            $list = $this->_news->getListSelect(['classify'=>1,'flag'=>1],true);
        }
        foreach ($list as $v){
            $v['uname'] = str_replace(" ","-",$v['name']);
            $v['create_time'] = date('m, Y',$v['create_time']);
        }
        return json_echo_msg(200,'chenggong',$list);
    }
    //新闻详情页
    public function view(){

        $id = input('name');
        $u = $_SERVER['REQUEST_URI'];

        if(strpos($u,'html')==false){
            $this->redirect('/news/'.$id.'.html',301);
        }
        $data = $this->_news->getOneByName(['id'=>$id]);
        $this->_news->up(['id'=>$data['id']],['pv'=>($data['pv']+1)]);
        $data['create_time'] = date('m, Y',$data['create_time']);
        $this->assign('data',$data);
        //推荐列表
        $likelist = $this->_news->getListSelect(['classify'=>1,'flag'=>1],false,3);
        foreach ($likelist as $v){
            $v['uname'] = str_replace(" ","-",$v['name']);
            $v['create_time'] = date('m, Y',$v['create_time']);
        }
        $this->assign('likelist',$likelist);
        //标签列表
        $tags = $this->_Label->getListSelect();
        $this->assign('tags',$tags);
        //上一个  下一个
        $data1 = $this->_news->getListSelect(['classify'=>1,'flag'=>1],true);
        $b = [];
        $c = [];
        $id = $data['id'];
        $kk=0;
        foreach ($data1 as $k=>$v){
            if($v['id']==$id){
                $kk = $k;
            }
        }
        foreach ($data1 as $k=>$v){
            if($k==$kk-1){
                $b = $v;
                $b['uname'] = str_replace(" ","-",$b['name']);
            }
        }
        foreach ($data1 as $k=>$v){
            if($k==$kk+1){
                $c = $v;
                $c['uname'] = str_replace(" ","-",$c['name']);
            }
        }
        $this->assign("b",$b);
        $this->assign("c",$c);
        return $this->fetch();
    }
}